package com.tbit.uqbike.client.remote.sms;


import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONObject;
import org.json.XML;

/**
 * @Author: chen
 * @Description:
 * @Date: 创建于 10:38 2020/10/12
 */
public class BSCXSmsApi {

    private final static Logger log = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
    /**
     * 接入密钥
     */
    private static final String userId = "367";
    /**
     * 接入密钥
     */
    private static final String name = "yy00158";
    /**
     * 接入密钥
     */
    private static final String password = "yy00158258";
    private static HttpClient client = null;

    /**
     * 维持单session
     */
    public static HttpClient getClient() {
        if (client == null) {
            MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
            client = new HttpClient(connectionManager);
            /**设置编码*/
            client.getParams().setContentCharset("UTF-8");
            /**请求超时*/
            client.getHttpConnectionManager().getParams().setConnectionTimeout(1000);
            /**读取超时*/
            client.getHttpConnectionManager().getParams().setSoTimeout(5000);
        }
        return client;
    }

    /**
     * 波思出行短信发送
     *
     * @param phone 手机号码
     * @param code  验证码
     */
    public static void sendSms(String phone, String code) {
        HttpClient client = getClient();
        PostMethod post = null;
        try {
            post = new PostMethod("http://47.95.231.135:8888/sms.aspx");
            NameValuePair phonePair = new NameValuePair("mobile", phone);
            NameValuePair contentPair = new NameValuePair("content", "【波思出行】您的验证码为" + code + "，请于5分钟内正确输入，如非本人操作，请忽略此短信。");
            NameValuePair userPair = new NameValuePair("userid", userId);
            NameValuePair accountPair = new NameValuePair("account", name);
            NameValuePair passwordPair = new NameValuePair("password", password);
            NameValuePair sendTimePair = new NameValuePair("sendTime", "");
            NameValuePair actionPair = new NameValuePair("action", "send");
            NameValuePair extnoPair = new NameValuePair("extno", "");
            post.setRequestBody(new NameValuePair[]{phonePair, contentPair, userPair, accountPair, passwordPair, sendTimePair, actionPair, extnoPair});
            client.executeMethod(post);
            String result = post.getResponseBodyAsString();
            JSONObject xmlJSONObj = XML.toJSONObject(result);
      /*if(xmlJSONObj.getJSONObject("returnsms").getString("returnstatus").equals("Success")){
          log.info("短信发送成功,手机号码="+phone);
      }else{
        log.info("短信发送失败,手机号码="+phone);
      }*/
            log.info(phone + "短信发送回复=" + xmlJSONObj.toString());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            post.releaseConnection();
        }
    }

    public static void main(String[] args) {
        sendSms("15079017029", "5566");
    }

}
